package com.itmg.analyze.jp.data.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.itmg.analyze.jp.bean.AnalyzeBean;
import com.itmg.analyze.jp.data.IDataAnalyze;
import com.itmg.vo.SearchResultItemVO;
import com.itmg.vo.SearchResultVO;
import com.itmg.vo.TargetDataVO;

/**
 *  dom解析amazon服务返回的xml文件
 * @author Yunhong Chen
 * @version 1.0
 * */
public class AmazonDataAnlyzeImpl implements IDataAnalyze {
	
	protected Logger log = Logger.getLogger(AmazonDataAnlyzeImpl.class.getName());

	public AmazonDataAnlyzeImpl() {
		
	}

	public SearchResultVO getSearchResultItemVO(TargetDataVO targetDataVO)
			throws Exception {
		Document document = AnalyzeBean.parseDocument(targetDataVO.getUrl());
		String sourceSiteName = targetDataVO.getSourceSiteName();
		SearchResultVO searchResultVO = new SearchResultVO();
		if(document == null)
			return searchResultVO;
		Element element = document.getDocumentElement();
		List<SearchResultItemVO> searchResultItemVOList = new ArrayList<SearchResultItemVO>();
		boolean isHaveNextdata = false;
		NodeList itemsNodeList = element.getElementsByTagName("Items");
		for(int i=0;i<itemsNodeList.getLength();i++)
		{
			Node node = itemsNodeList.item(i);
			if(node.getNodeName().equals("Items"))
			{
				NodeList firstNodeList = node.getChildNodes();
				for(int m=0;m<firstNodeList.getLength();m++)
				{
					Node firstNode = firstNodeList.item(m);
					if(firstNode.getNodeName().equals("TotalResults")){
						String totalResults = firstNode.getTextContent().trim();
						if(!"".equals(totalResults)){
							searchResultVO.setTotalResultNumber(Integer.parseInt(totalResults));
							isHaveNextdata = true;
						}
					}
					if(firstNode.getNodeName().equals("Item") && isHaveNextdata==true)
					{
						SearchResultItemVO searchResultItemVO = new SearchResultItemVO();
						NodeList nodeList = firstNode.getChildNodes();
						for(int j=0;j<nodeList.getLength();j++){
							Node itemNode = nodeList.item(j);
							//商品ID
							if(itemNode.getNodeName().equals("ASIN")){
								String productID = itemNode.getTextContent().trim();
								if(!"".equals(productID)){
									searchResultItemVO.setProductID(productID);
								}
							}
							//商品的detail页面
							if(itemNode.getNodeName().equals("DetailPageURL")){
								String productUrl = itemNode.getTextContent().trim();
								if(!"".equals(productUrl)){
									searchResultItemVO.setProductUrl(productUrl);
								}
							}
							//商品小图片的URL
							if(itemNode.getNodeName().equals("SmallImage")){
								NodeList smallImageNodeList = itemNode.getChildNodes();
								for(int g=0;g<smallImageNodeList.getLength();g++){
									Node smallImageNode = smallImageNodeList.item(g);
									if(smallImageNode.getNodeName().equals("URL")){
										String smallImageUrl = smallImageNode.getTextContent().trim();
										if(!"".equals(smallImageUrl)){
											searchResultItemVO.setSmallImageUrl(smallImageUrl);
										}
									}
								}
								
							}
							//商品大图片的URL
							if(itemNode.getNodeName().equals("LargeImage")){
								NodeList bigImageNodeList = itemNode.getChildNodes();
								for(int g=0;g<bigImageNodeList.getLength();g++){
									Node bigImageNode = bigImageNodeList.item(g);
									if(bigImageNode.getNodeName().equals("URL")){
										String bigImageUrl = bigImageNode.getTextContent().trim();
										if(!"".equals(bigImageUrl)){
											searchResultItemVO.setBigImageUrl(bigImageUrl);
										}
									}
								}
							}
							if(itemNode.getNodeName().equals("ItemAttributes")){
								NodeList itemNodeList = itemNode.getChildNodes();
								for(int k=0;k<itemNodeList.getLength();k++){
									Node itemAttributeNode = itemNodeList.item(k);
									if(itemAttributeNode.getNodeName().equals("ListPrice")){
										NodeList priceNodeList = itemAttributeNode.getChildNodes();
										for(int n=0;n<priceNodeList.getLength();n++){
											Node priceNode = priceNodeList.item(n);
											if(priceNode.getNodeName().equals("FormattedPrice")){
												String lowestPrice = priceNode.getTextContent().trim();
												if(!"".equals(lowestPrice)){
													lowestPrice = lowestPrice.substring(2, lowestPrice.length()).trim();
													if(lowestPrice.indexOf(",") != -1){
														lowestPrice = lowestPrice.replace(",", "");
													}
													searchResultItemVO.setLowestPrice(Integer.parseInt(lowestPrice));
												}
											}
										}
									}
									if(itemAttributeNode.getNodeName().equals("Manufacturer")){
										String makerName = itemAttributeNode.getTextContent().trim();
										if(!"".equals(makerName)){
											searchResultItemVO.setMakerName(makerName);
										}
									}
									if(itemAttributeNode.getNodeName().equals("Title")){
										String productName = itemAttributeNode.getTextContent().trim();
										if(!"".equals(productName)){
											searchResultItemVO.setProductName(productName);
											searchResultItemVO.setComment(productName);
										}
									}
								}
							}
						}
						String md5 = searchResultItemVO.getMyProductMd5();
						searchResultItemVO.setProductMd5(md5);
						searchResultItemVO.setSourceSiteName(sourceSiteName);
						searchResultItemVOList.add(searchResultItemVO);
					}
				}
			}
		}
		searchResultVO.setResultItemList(searchResultItemVOList);
		return searchResultVO;
	}

}
